package com.clean.cleantotal.compores.app

import android.os.Bundle
import androidx.core.view.isVisible
import com.clean.cleantotal.R
import com.clean.cleantotal.base.BaseActivity
import com.clean.cleantotal.bean.DataUtils
import com.clean.cleantotal.bean.FunType
import com.clean.cleantotal.bean.TikBean
import com.clean.cleantotal.databinding.ActivityScanAppBinding
import com.clean.cleantotal.compores.delete.DeleteActivity
import com.clean.cleantotal.utils.FormatterSize
import com.clean.cleantotal.utils.Utils
import com.drake.brv.utils.bindingAdapter
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup

class ScanAppActivity : BaseActivity<ActivityScanAppBinding>() {
    override val viewBinding: ActivityScanAppBinding
        get() = ActivityScanAppBinding.inflate(layoutInflater)

    private var funType: FunType? = null
    private var title = ""
    override fun initParams(extras: Bundle?) {
        super.initParams(extras)
        extras?.let {
            funType = it.getSerializable("funType") as FunType?
            title = it.getString("title", "tik tok")
        }
    }

    private var selectList = HashSet<TikBean>()
    override fun initView() {

        Utils.showScanDialog(this, funType, mBinding.loadingLayout.scanLayout, { animal = it }) {

        }
        animal?.duration = 3000

        mBinding.back.setOnClickListener { onBackPressed() }
        mBinding.title.text = title
        val list = DataUtils.getTikList(funType)
        mBinding.recyclerView.linear().setup {
            addType<TikBean> { R.layout.item_tik_layout }
            onClick(R.id.tikLayout) {
                val model = getModel<TikBean>()
                model.isSelect = !model.isSelect
                if (model.isSelect) {
                    selectList.add(model)
                } else {
                    selectList.remove(model)
                }
                mBinding.recyclerView.bindingAdapter.notifyItemChanged(position)
            }
        }.models = list

        mBinding.includedLayout.tvTitle.text = "Found"
        mBinding.includedLayout.tvContent.text = "Clean up the trash immediately if \n you find it!"
        mBinding.includedLayout.ivFinish.isVisible = false
        val size = FormatterSize.formatFileSizeSplit(this, list.sumOf { it.size })
        mBinding.includedLayout.tvSize.text = size.first()
        mBinding.includedLayout.tvUnit.text = size.last()
        mBinding.includedLayout.yellowYuan.isVisible = true

        mBinding.okLayout.setOnClickListener {
            Utils.selectTikList.clear()
            Utils.selectTikList.addAll(selectList)
            openActivity(DeleteActivity::class.java, Bundle().apply {
                putSerializable("funType", funType)
                putString("title", title)
            })
            finish()
        }
    }

}